/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LibraryPanel.java
 *
 * Created on Mar 30, 2011, 8:12:16 PM
 */

package org.libarae.client;

import java.util.Vector;
import org.libarae.client.threads.BookSearchThread;
import org.libarae.client.threads.HasBookSearchThread;
import org.libarae.communication.common.Book;
import org.libarae.communication.common.Book.BookStatus;
import org.libarae.communication.request.BookSearchRequest;

/**
 *
 * @author Casey, Ben
 */
@SuppressWarnings("serial")
public class LibraryPanel extends javax.swing.JPanel implements HasBookSearchThread {

    private int mPageNumber = 0;
    private int mPageSize = 15;
    @SuppressWarnings("unused")
	private int mNumPages;

    private Vector<Book> mBooks;

    private MainScreen mMainScreen;

    private BookSearchRequest mBookSearchRequest;

    /** Creates new form LibraryPanel */
    public LibraryPanel() {
        initComponents();
    }

    public LibraryPanel(MainScreen mMainScreen)
    {
        this.mMainScreen = mMainScreen;
        initComponents();
    }

    public void search(String omniSearch)
    {
        BookSearchRequest r = new BookSearchRequest();

        r.setOmniSearch(omniSearch);

        r.setSearchPageNumber(mPageNumber);
        r.setSearchPageSize(mPageSize);

        mBookSearchRequest = r;

        BookSearchThread t = new BookSearchThread(mBookSearchRequest, this);
        t.start();
    }

    public void search(String title, String author, String isbn, String publisher, String pubDate, String summary, String omniSearch, String status)
    {
        BookSearchRequest r = new BookSearchRequest();

        r.setTitle(title);
        r.setAuthor(author);
        r.setIsbn(isbn);
        r.setPublisher(publisher);
        r.setYear(pubDate);
        r.setSummary(summary);
        r.setOmniSearch(omniSearch);
        
        if(status.equals("Any"))
        {
            r.setStatus(null);
        }
        else if(status.equals("Available"))
        {
            r.setStatus(BookStatus.AVAILABLE);
        }
        else if(status.equals("Pending"))
        {
            r.setStatus(BookStatus.PENDING);
        }
        else if(status.equals("Checked Out"))
        {
            r.setStatus(BookStatus.CHECKED_OUT);
        }

        r.setSearchPageNumber(mPageNumber);
        r.setSearchPageSize(mPageSize);

        mBookSearchRequest = r;

        BookSearchThread t = new BookSearchThread(mBookSearchRequest, this);
        t.start();
    }

    public void refreshList()
    {
        if(mBookSearchRequest == null)
        {
            search("");
            return;
        }
        
        mBookSearchRequest.setSearchPageNumber(mPageNumber);
        mBookSearchRequest.setSearchPageSize(mPageSize);

        BookSearchThread t = new BookSearchThread(mBookSearchRequest, this);
        t.start();
    }

    public void redisplayList(Vector<Book> mBooks)
    {
        this.mBooks = mBooks;
        redisplayList();
    }

    private void redisplayList()
    {
        blpLibrary.setBooks(mBooks);

        this.revalidate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnPrev = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();
        scrLibrary = new javax.swing.JScrollPane();
        blpLibrary = new org.libarae.client.BookListPanel(mMainScreen);

        btnPrev.setText("Previous");
        btnPrev.setEnabled(false);
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        scrLibrary.setViewportView(blpLibrary);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnPrev)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 179, Short.MAX_VALUE)
                        .addComponent(btnNext))
                    .addComponent(scrLibrary, javax.swing.GroupLayout.PREFERRED_SIZE, 307, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPrev)
                    .addComponent(btnNext))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(scrLibrary, javax.swing.GroupLayout.PREFERRED_SIZE, 107, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        // Reduce the page number and then re-request the book list from the server.
        mPageNumber--;
        if(mPageNumber == 0)
        {
            btnPrev.setEnabled(false);
        }

        refreshList();
    }//GEN-LAST:event_btnPrevActionPerformed

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        // Increase the page number and then re-request the book list from the server.
        mPageNumber++;
        if(mPageNumber == 1)
        {
            btnPrev.setEnabled(true);
        }

        refreshList();
    }//GEN-LAST:event_btnNextActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.libarae.client.BookListPanel blpLibrary;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private javax.swing.JScrollPane scrLibrary;
    // End of variables declaration//GEN-END:variables

}
